package com.dly.blog.enums;

import cn.hutool.core.lang.func.Func1;
import com.dly.blog.domian.ro.LoginRO;
import com.dly.blog.token.AccountToken;
import com.dly.blog.token.EmailToken;
import com.dly.blog.utile.SpringContextHolder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.core.Authentication;

import java.util.function.Function;

/**
 * @Author: DaiLiYu
 * @Date: 2023/4/25 21:56
 */
@Getter
public enum LoginType {

    ACCOUNT((loginRO)->SpringContextHolder.getBean(ProviderManager.class).authenticate(
            new AccountToken(
                            loginRO.getUsername(),
                            loginRO.getPassword())
            )
    ),

    EMAIL(
            (loginRO)->SpringContextHolder.getBean(ProviderManager.class).authenticate(
                    new EmailToken(
                            loginRO.getUsername(),
                            loginRO.getPassword())
            )
    );

    LoginType(Function<LoginRO, Authentication> function){
        this.authentication = function;
    }

    private final Function<LoginRO, Authentication> authentication;
}
